/*
 * TraceabilityTreeBuilder.java
 *
 * $Id: TraceabilityTreeBuilder.java,v 1.2 2008-06-26 10:42:17 mario Exp $
 */
package org.ceteca.explica.core.traceability.tree;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import org.ceteca.explica.core.traceability.InterfaceTraceabilityErrors;
import org.ceteca.explica.core.traceability.TracerException;
import org.ceteca.explica.core.traceability.tree.parser.TraceabilityTreeParser;

/**
 * Factory class that gives access to the Traceability Tree Construction
 * mechanism. 
 * <br/>
 * Responsibilities:
 * <ul>
 * 	<li>Instantiation of the Traceability Files Parser. </li>
 *  <li>Façade to the Traceability Tree Construction.</li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class TraceabilityTreeBuilder {
	/** Unique instance of the TraceabilityTreeBuilder class. Singleton pattern */
	private static TraceabilityTreeBuilder instance = null;

	/**
	 * Private default constructor por TraceabilityTreeBuilder objects.
	 */
	private TraceabilityTreeBuilder() {
	}
	
	/** Returns the unique instance of the TraceabilityTreeBuilder class.
	 * Implements the singleton dessign pattern.
	 * @return TraceabilityTreeBuilder, unique instance of TraceabilityTreeBuilder.
	 */
	public static final TraceabilityTreeBuilder getInstance() {
		if (instance == null)
			instance = new TraceabilityTreeBuilder();
		return instance;
	}
	
	/**
	 * Parses a traceability tree file and builds a traceability tree 
	 * based on this parsed file.
	 * @param xmlContent byte[], byte content stored in the traceability file. 
	 * @return TraceabilityTree, traceability tree corresponding to the
	 * parsed contents of the traceability file.
	 * @throws TracerException, in case any error takes place during the
	 * parsing of the traceability file or during the building of the 
	 * traceability tree object that will store the traceability data.
	 */
	public TraceabilityTree buildTraceabilityTree(byte[] xmlContent) throws TracerException {
		try {
			TraceabilityTreeParser parser = new TraceabilityTreeParser(xmlContent);
			return parser.getTrTree();
		}
		catch (ParserConfigurationException pEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, pEx);
		}
		catch (IOException ioEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, ioEx);
		}
		catch (SAXException saxEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, saxEx);
		}
	}
	
	/**
	 * Parses a traceability tree file and builds a traceability tree 
	 * based on this parsed file.
	 * @param xmlFilename String, file path to the traceability file. 
	 * @return TraceabilityTree, traceability tree corresponding to the
	 * parsed contents of the traceability file.
	 * @throws TracerException, in case any error takes place during the
	 * parsing of the traceability file or during the building of the 
	 * traceability tree object that will store the traceability data.
	 */
	public TraceabilityTree buildTraceabilityTree(String xmlFilename) throws TracerException {
		try {
			TraceabilityTreeParser parser = new TraceabilityTreeParser(xmlFilename);
			return parser.getTrTree();
		}
		catch (ParserConfigurationException pEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, pEx);
		}
		catch (IOException ioEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, ioEx);
		}
		catch (SAXException saxEx) {
			throw new TracerException(InterfaceTraceabilityErrors.ERR_TRACEABILITY_PARSING_GENERAL_ERROR, saxEx);
		}
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: TraceabilityTreeBuilder.java,v 1.2 2008-06-26 10:42:17 mario Exp $";
	}
}
